﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DAL;
using Model;
using Common;
using System.IO;
using System.Xml;


namespace BLL
{
    /// <summary>
    /// 企业付款
    /// </summary>
    public class EnterprisePaymentBLL
    {
        /// <summary>
        /// 付款
        /// </summary>
        /// <returns></returns>
        public ReturnMessages Pay(string openid)
        {
            ReturnMessages rm = new ReturnMessages();
            try
            {
                //获取配置信息
                XmlNodeList xmllist = XmlHelper.ReadNodes(Directory.GetCurrentDirectory() + "/wwwroot/config/weixin.config", "configuration");
                string appid = xmllist[0].InnerText;
                string mchid = xmllist[2].InnerText;
                string apikey = xmllist[3].InnerText;
                string sslcert_path = xmllist[4].InnerText;
                string sslcert_password = xmllist[5].InnerText;
                string server_ip = xmllist[6].InnerText;

                EnterprisePayment_post model_post = new EnterprisePayment_post();
                model_post.mch_appid = appid;
                model_post.spbill_create_ip = server_ip;
                model_post.nonce_str = Utils.GetCheckCode(32);
                model_post.mchid = mchid;
                model_post.partner_trade_no = mchid + DateTime.Now.ToString("yyMMddHHmmss") + Utils.Number(4);
                model_post.openid = openid;
                model_post.check_name = "NO_CHECK";//不校验真实姓名
                model_post.amount = 100;
                model_post.desc = "企业付款测试";
                model_post.sign = WeiXinHelper.MakeSign<EnterprisePayment_post>(model_post, apikey);

                string xml = HttpServiceHelper.Post_cert(XmlHelper.Serializer_WeiXin<EnterprisePayment_post>(model_post), "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers", sslcert_path, sslcert_password).Result;
                EnterprisePayment_return model_return = XmlHelper.DeserializeXmlToObject_WeiXin<EnterprisePayment_return>(xml);

                if (model_return.return_code == "SUCCESS")
                {
                    if (model_return.result_code == "SUCCESS")
                    {
                        rm.code = 200;
                    }
                    else
                    {
                        LogHelper.Error("企业付款支付失败,错误代码：" + model_return.err_code, model_return.err_code_des);
                        rm.code = 400;
                        rm.msg = model_return.err_code_des;
                    }
                }
                else
                {
                    LogHelper.Error("企业付款发送失败", model_return.return_msg);
                    rm.code = 400;
                    rm.msg = model_return.return_msg;
                }


                return rm;
            }
            catch (Exception e)
            {
                rm.code = 500;
                rm.msg = "server error";
                LogHelper.Error(this.GetType().FullName, e.ToString());
                return rm;
            }
        }



    }
}
